<?php
session_start();
$_SESSION["error"]="";

if (isset($_REQUEST["txtStudentID"]))
{
	$sDatabaseName = "courses";
	$sUser = "root";
	$sPassword="quang6van21";
	$sServer="localhost";
	$sTableName="f2014la";
	$sTeamwork="1";
	
	$sqlConnect = new mysqli($sServer,$sUser,$sPassword,$sDatabaseName);
	
	/*echo "<?xml version=\"1.0\" encoding=\"utf-8\"?><result>";*/
	//Check connection
	if ($sqlConnect ->connect_error) {
		$_SESSION["error"]="Can't connect to database";
	}
	else
	{
		$sQuery = "SELECT `Name` FROM `".$sTableName."` WHERE `StudentID`=".$_REQUEST["txtStudentID"];
		$sqlResult=$sqlConnect->query($sQuery);
		$numrow = $sqlResult->num_rows;
		
		if ($numrow==0)
		{
			$_SESSION['error']="No students found";
		}
		else {if ($numrow==1)
			{	
				$row=$sqlResult->fetch_row();
				$sQuery = "UPDATE `".$sTableName."` SET `".$sTeamwork."`=".$_REQUEST["txtScore"]." WHERE `StudentID`=".$_REQUEST["txtStudentID"];
				//echo $sQuery;
				$sqlResult = $sqlConnect->query($sQuery);
				$_SESSION["StudentID"]=$_REQUEST["txtStudentID"];
				$_SESSION["Score"]=$_REQUEST["txtScore"];
				$_SESSION["Name"]=$row[0];
			}
		}
}
/*	*/
$sqlConnect->close();
}

?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Add Student Score</title>
</head>

<body>
<form name="test" type="post" action="">
<input type="text" name="txtStudentID" width="50"/>
<input type="text" name="txtScore" width="40"/>
<button type="submit">Submit</button>
</form>
<span><?php if (!($_SESSION["error"]==""))
{
	echo ($_SESSION["error"]);
}
else if (isset($_SESSION["Name"])){
	echo ($_SESSION["Name"].": ".$_SESSION["Score"]);
}
 ?></span>
</body>

</html>